cmake_minimum_required(VERSION 3.16)

project(AutoPack VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置Qt相关配置
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 查找Qt6
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui Network Svg)

# 设置包含目录
include_directories(forms/inc)

# 设置源文件
set(SOURCES
    main.cpp
    forms/src/widget.cpp
    forms/src/autoPackage.cpp
)

set(HEADERS
    forms/inc/widget.h
    forms/inc/autoPackage.h
    forms/inc/issSource.h
)

# 设置资源文件
set(RESOURCES
    forms/resources.qrc
)

# 创建可执行文件
add_executable(AutoPack
    ${SOURCES}
    ${HEADERS}
    ${RESOURCES}
)

# 链接Qt库
target_link_libraries(AutoPack
    Qt6::Core
    Qt6::Widgets
    Qt6::Gui
    Qt6::Network
    Qt6::Svg
)

# 设置Windows特定配置
if(WIN32)
    # 设置应用程序图标
    set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/icons/package.ico")
    # 生成资源文件内容
    set(RC_CONTENT "IDI_ICON1 ICON DISCARDABLE \"${CMAKE_CURRENT_SOURCE_DIR}/icons/package.ico\"

1 VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
FILEFLAGS 0x0L
FILEOS 0x40004L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
    BLOCK \"StringFileInfo\"
    BEGIN
        BLOCK \"040904b0\"
        BEGIN
            VALUE \"CompanyName\", \"AutoPack\"
            VALUE \"FileDescription\", \"自动打包工具\"
            VALUE \"FileVersion\", \"1.0.0.0\"
            VALUE \"InternalName\", \"AutoPack\"
            VALUE \"LegalCopyright\", \"Copyright\"
            VALUE \"OriginalFilename\", \"AutoPack.exe\"
            VALUE \"ProductName\", \"AutoPack\"
            VALUE \"ProductVersion\", \"1.0.0.0\"
        END
    END
    BLOCK \"VarFileInfo\"
    BEGIN
        VALUE \"Translation\", 0x409, 1200
    END
END")
    
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/windows_resource.rc" "${RC_CONTENT}")
    target_sources(AutoPack PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/windows_resource.rc")
    
    # 设置为Windows应用程序（不显示控制台窗口）
    set_target_properties(AutoPack PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
endif()

# 设置目标属性
set_target_properties(AutoPack PROPERTIES
    OUTPUT_NAME "AutoPack"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/AutoPack"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/AutoPack"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/AutoPack"
)

# 安装规则
install(TARGETS AutoPack
    BUNDLE DESTINATION .
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
)

# 添加自定义命令复制图标文件到输出目录
add_custom_command(TARGET AutoPack POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:AutoPack>/icons
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/icons/tct.ico
        $<TARGET_FILE_DIR:AutoPack>/icons/tct.ico
    COMMENT "Copying icon file to output directory"
)

# 如果是Release构建，则自动部署Qt依赖
if(WIN32)
    # 查找windeployqt
    get_target_property(QT6_QMAKE_EXECUTABLE Qt6::qmake IMPORTED_LOCATION)
    get_filename_component(QT6_WINDEPLOYQT_EXECUTABLE ${QT6_QMAKE_EXECUTABLE} PATH)
    set(QT6_WINDEPLOYQT_EXECUTABLE "${QT6_WINDEPLOYQT_EXECUTABLE}/windeployqt.exe")
    
    if(EXISTS ${QT6_WINDEPLOYQT_EXECUTABLE})
        add_custom_command(TARGET AutoPack POST_BUILD
            COMMAND ${QT6_WINDEPLOYQT_EXECUTABLE} --verbose 0 --no-translations --no-system-d3d-compiler --no-opengl-sw $<TARGET_FILE:AutoPack>
            WORKING_DIRECTORY $<TARGET_FILE_DIR:AutoPack>
            COMMENT "Deploying Qt libraries")
    endif()
endif()
